package com.jt.www.admin.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.jt.www.admin.order_pc.service.OrgMarketerService;
import com.jt.www.admin.user.service.UserListFormService;
import com.jt.www.domain.po.OrgMarketer;
import com.jt.www.enums.endorse.PersonTypeEnum;
import com.jt.www.model.remote.user.UserListDownloadVo;
import com.jt.www.model.reps.GenericListResponse;
import com.jt.www.model.vo.EduOrgListVO;
import com.jt.www.model.vo.EduOrgsVO;
import com.jt.www.model.vo.UserInfoVo;
import com.jt.www.remote.UserClient;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserListFormServiceImpl implements UserListFormService {

    public static final Logger log = LoggerFactory.getLogger(UserListFormServiceImpl.class);

    @Autowired
    private UserClient userClient;
    @Autowired
    private OrgMarketerService orgMarketerService;

    @Override
    public List<UserListDownloadVo> downUserList(UserInfoVo userInfoVo) throws Exception {

        String findcode = "EDU_USER :" + System.currentTimeMillis();
        log.debug(findcode + "前端传递参数 ，{}", JSON.toJSONString(userInfoVo));
        ResponseEntity<GenericListResponse<UserListDownloadVo>> resuts = userClient.queryUserInfoDownList(JSON.toJSONString(userInfoVo));
        if (resuts.getStatusCode().equals(HttpStatus.OK)) {

            //用户角色替换为对应描述
            if (resuts.getBody().getList() != null && resuts.getBody().getList().size() > 0) {
                for (UserListDownloadVo vo : resuts.getBody().getList()) {
                    if (vo != null) {
                        vo.setPersonType(StringUtils.isNotBlank(vo.getPersonType()) ? PersonTypeEnum.getMsgByCode(vo.getPersonType()) : "");
                    }
                }
            }

            //查询成功返回用户信息列表
            return resuts.getBody().getList();
        }
        return Lists.newArrayList();
    }


    @Override
    public ResponseEntity<GenericListResponse<EduOrgsVO>> queryOrgInfoList(EduOrgListVO eduOrgListVO) {
        ResponseEntity<GenericListResponse<EduOrgsVO>> resuts = null;
        try {
            resuts = userClient.queryOrgInfoList(eduOrgListVO);
        } catch (Exception e) {
            return GenericListResponse.listNoCount(Lists.newArrayList());
        }
        List<EduOrgsVO> orgList;
        if (null != resuts && resuts.getStatusCode().equals(HttpStatus.OK) && CollectionUtils.isNotEmpty(orgList = resuts.getBody().getList())) {

            //用查询结果中的机构userCode   查询机构的推广人配置
            List<String> userCodes = orgList.stream().map(e -> e.getUserCode()).collect(Collectors.toList());
            List<OrgMarketer> orgMarketers = orgMarketerService.selectByOrgUserCodes(userCodes);
            if (CollectionUtils.isNotEmpty(orgMarketers)) {
                for (OrgMarketer orgMarketer : orgMarketers) {
                    for (EduOrgsVO org : orgList) {
                        if (orgMarketer.getOrgUserCode().equals(org.getUserCode())) {
                            //添加推广人名称
                            org.setMarketerName(orgMarketer.getMarketerName());
                            org.setMarketerNo(orgMarketer.getMarketerNo());
                            org.setMarketerUserCode(orgMarketer.getMarketerUserCode());
                        }
                    }
                }
            }
            //查询成功返回用户信息列表
            return resuts;
        }
        return GenericListResponse.listNoCount(Lists.newArrayList());
    }
}
